<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接单状态 " prop="receivingOrder">
        <el-select v-model="queryParams.receivingOrder" placeholder="请选择接单状态 " clearable>
          <el-option
            v-for="dict in dict.type.receiving_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.user_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-input
          v-model="queryParams.level"
          placeholder="请输入级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工龄" prop="workYears">
        <el-input
          v-model="queryParams.workYears"
          placeholder="请输入工龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="服务区域" prop="serviceAreas">
        <el-input
          v-model="queryParams.serviceAreas"
          placeholder="请输入服务区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="appointmentTime">
        <el-date-picker clearable
                        v-model="queryParams.appointmentTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['function:technician:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['function:technician:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['function:technician:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['function:technician:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="technicianList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="个人介绍" align="center" prop="introduction" />
      <el-table-column label="专业资格证书(JSON)" align="center" prop="skillCertificate" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.skillCertificate" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="平台资格证书(JSON)" align="center" prop="platformCertificate" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.platformCertificate" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="经验证明照片(JSON)" align="center" prop="experiencePhotos" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.experiencePhotos" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="接单状态 " align="center" prop="receivingOrder">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.receiving_status" :value="scope.row.receivingOrder"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_state" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column label="总评分" align="center" prop="totalScore" />
      <el-table-column label="评价次数" align="center" prop="scoreCount" />
      <el-table-column label="平均评分" align="center" prop="avgScore" />
      <el-table-column label="完成订单数" align="center" prop="completeOrderCount" />
      <el-table-column label="工时" align="center" prop="workingHours" />
      <el-table-column label="工龄" align="center" prop="workYears" />
      <el-table-column label="服务类型" align="center" prop="serviceTypeIds" />

      <el-table-column label="服务区域" align="center" prop="serviceAreas" />
      <el-table-column label="区域" align="center" prop="addressName" />
      <el-table-column label="头像" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" align="center" prop="appointmentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appointmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="审核备注" align="center" prop="auditRemark" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时段价" align="center" prop="hourPrice" />
      <el-table-column label="级别" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.technician_level" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['function:technician:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
            v-if="scope.row.auditStatus === 1"
            v-hasPermi="['function:garage:approve']"
          >审批
          </el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['function:technician:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改技师对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="个人介绍" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="专业资格证书(JSON)" prop="skillCertificate">
          <image-upload v-model="form.skillCertificate"/>
        </el-form-item>
        <el-form-item label="平台资格证书(JSON)" prop="platformCertificate">
          <image-upload v-model="form.platformCertificate"/>
        </el-form-item>
        <el-form-item label="经验证明照片(JSON)" prop="experiencePhotos">
          <image-upload v-model="form.experiencePhotos"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.audit_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接单状态 " prop="receivingOrder">
          <el-select v-model="form.receivingOrder" placeholder="请选择接单状态 ">
            <el-option
              v-for="dict in dict.type.receiving_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.user_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时段价" prop="hourPrice">
          <el-input v-model="form.hourPrice" placeholder="请输入时段价" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select v-model="form.level" placeholder="请选择级别">
            <el-option
              v-for="dict in dict.type.technician_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总评分" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入总评分" />
        </el-form-item>
        <el-form-item label="评价次数" prop="scoreCount">
          <el-input v-model="form.scoreCount" placeholder="请输入评价次数" />
        </el-form-item>
        <el-form-item label="平均评分" prop="avgScore">
          <el-input v-model="form.avgScore" placeholder="请输入平均评分" />
        </el-form-item>
        <el-form-item label="完成订单数" prop="completeOrderCount">
          <el-input v-model="form.completeOrderCount" placeholder="请输入完成订单数" />
        </el-form-item>
        <el-form-item label="工时" prop="workingHours">
          <el-input v-model="form.workingHours" placeholder="请输入工时" />
        </el-form-item>
        <el-form-item label="工龄" prop="workYears">
          <el-input v-model="form.workYears" placeholder="请输入工龄" />
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceTypeIds">
          <el-input v-model="form.serviceTypeIds" placeholder="请输入工龄" />
        </el-form-item>
        <el-form-item label="服务区域" prop="serviceAreas">
          <el-input v-model="form.serviceAreas" placeholder="请输入服务区域" />
        </el-form-item>
        <el-form-item label="区域" prop="addressName">
          <el-input v-model="form.addressName" placeholder="请输入区域" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="预约时间" prop="appointmentTime">
          <el-date-picker clearable
                          v-model="form.appointmentTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="form.auditRemark" placeholder="请输入审核备注" />
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
                          v-model="form.auditTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="approvaLopen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="个人介绍" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="专业资格证书(JSON)" prop="skillCertificate">
          <image-upload v-model="form.skillCertificate"/>
        </el-form-item>
        <el-form-item label="平台资格证书(JSON)" prop="platformCertificate">
          <image-upload v-model="form.platformCertificate"/>
        </el-form-item>
        <el-form-item label="经验证明照片(JSON)" prop="experiencePhotos">
          <image-upload v-model="form.experiencePhotos"/>
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceTypeIds">
          <el-input v-model="form.serviceTypeIds" placeholder="请输入服务类型" />
        </el-form-item>
        <el-form-item label="服务区域" prop="serviceAreas">
          <el-input v-model="form.serviceAreas" placeholder="请输入服务区域" />
        </el-form-item>
        <el-form-item label="区域" prop="addressName">
          <el-input v-model="form.addressName" placeholder="请输入区域" />
        </el-form-item>
        <el-form-item label="时段价" prop="hourPrice">
          <el-input v-model="form.hourPrice" placeholder="请输入时段价" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select v-model="form.level" placeholder="请选择级别">
            <el-option
              v-for="dict in dict.type.technician_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工龄" prop="workYears">
          <el-input v-model="form.workYears" placeholder="请输入工龄" />
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="form.auditRemark" placeholder="请输入审核备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="approvaForm(2)">审批通过</el-button>
        <el-button type="primary" @click="approvaForm(3)">拒 绝</el-button>
        <el-button @click="approvaCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTechnician, getTechnician, delTechnician, addTechnician, updateTechnician,putCheck } from "@/api/function/technician";

export default {
  name: "Technician",
  dicts: ['audit_status', 'receiving_status', 'service_type', 'technician_level','user_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 技师表格数据
      technicianList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      approvaLopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        introduction: null,
        skillCertificate: null,
        platformCertificate: null,
        experiencePhotos: null,
        auditStatus: null,
        receivingOrder: null,
        level: null,
        totalScore: null,
        scoreCount: null,
        avgScore: null,
        completeOrderCount: null,
        workingHours: null,
        workYears: null,
        serviceTypeIds: null,
        serviceAreas: null,
        addressName: null,
        avatar: null,
        appointmentTime: null,
        name: null,
        phone: null,
        auditRemark: null,
        auditTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询技师列表 */
    getList() {
      this.loading = true;
      listTechnician(this.queryParams).then(response => {
        this.technicianList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    approvaCancel() {
      this.approvaLopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        introduction: null,
        skillCertificate: null,
        platformCertificate: null,
        experiencePhotos: null,
        auditStatus: null,
        receivingOrder: null,
        level: null,
        totalScore: null,
        scoreCount: null,
        avgScore: null,
        completeOrderCount: null,
        workingHours: null,
        workYears: null,
        serviceTypeIds: null,
        serviceAreas: null,
        addressName: null,
        avatar: null,
        appointmentTime: null,
        name: null,
        phone: null,
        auditRemark: null,
        auditTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加技师";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTechnician(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改技师";
      });
    },
    /** 修改按钮操作 */
    handleApprove(row) {
      this.reset();
      const id = row.id || this.ids
      getTechnician(id).then(response => {
        this.form = response.data;
        this.approvaLopen = true;
        this.title = "审批技师";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTechnician(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechnician(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审批按钮 */
    approvaForm(i) {
      this.form.auditStatus=i;
      console.log(this.form.auditStatus)
      if (i === 3 && !this.form.auditRemark) {
        this.$modal.msgError("审批不通过时备注必填");
        return;
      }
      if (i === 2){
        if (!this.form.level){
          this.$modal.msgError("级别必填");
          return;
        }
        if (!this.form.workYears){
          this.$modal.msgError("工龄必填");
          return;
        }
      }
      putCheck(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.approvaLopen = false;
        this.getList();
      });


    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除技师编号为"' + ids + '"的数据项？').then(function() {
        return delTechnician(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('function/technician/export', {
        ...this.queryParams
      }, `technician_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
